<template>
  <Head />
  <Bread :title="$t('menu3')" :rightText="$t('withdrawalsRecord')" @onRecord="onRecord" />

  <div v-for="(item, index) in walletList" :key="index" class="wallet">
    <img :src="item.logo" alt="" class="logo" />
    <div class="content">
      <div class="top">
        <span>{{ item.symbol }}</span>
        <span>{{ formatMoney(item.balance) }}</span>
      </div>
      <div class="bottom">
        <span>{{ item.exchangeSymbol }}</span>
        <span>≈$ {{ formatMoney(item.exchangeAmount) }}
          <!-- <i
            :class="[
              'diffFee',
              Number(item.exchangeRate) > 0 ? 'green' : 'red'
            ]"
            >（$
            {{
              Number(item.exchangeRate) > 0
                ? `+${formatMoney(item.exchangeRate)}`
                : formatMoney(item.exchangeRate)
            }}）</i
          > -->
        </span>
      </div>
    </div>
    <div @click="onWithdraw(item)" class="withdraw">
      <van-icon name="balance-o" />
    </div>
    <div @click="goUrl(item.symbol)" class="right">
      <van-icon name="arrow" />
    </div>
  </div>

  <van-popup v-model:show="showBottom" position="bottom" class="WithdrawPopup" :closeable="true" :style="{
    padding: '17px 20px 40px',
    background: '#11202f',
    borderRadius: '10px 10px 0 0',
  }">
    <van-form @submit="onSubmit" class="form">
      <p>{{ $t("withdraw") }}</p>
      <van-cell-group inset>
        <van-field :style="{ margin: 0 }" type="number" v-model="params.money" :placeholder="$t('pleaseEnter')" />
      </van-cell-group>
      <div class="submit">
        <van-button class="btn" @click="onSubmit" :loading="loading" :loading-text="$t('pledge')">
          {{ $t("withdraw") }}
        </van-button>
      </div>
    </van-form>
  </van-popup>
</template>

<script setup lang="ts">
import { formatMoney } from "@/utils/tool";
import { Async_myWallet, Async_withdraw } from "@/server/user";
import { IWalletItem } from "@/model/user";
import { showToast } from "vant";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const router = useRouter();



const walletList = ref<IWalletItem[]>([]);
let showBottom = ref(false)
let loading = ref(false)
let walletObj = ref({
  balance: 0,
  symbol: ""
})
let params = ref({
  coin: '',
  money: "",
})

const getList = async () => {
  try {
    let res: IWalletItem[] = await Async_myWallet();
    if (res) {
      walletList.value = res || [];
    }
  } catch (error) {
    console.error(error);
  }
};

onMounted(() => {
  getList();
});

const goUrl = coin => {
  router.push({
    path: "/walletDetails",
    query: {
      coin
    }
  });
};



const onRecord = () => {
  router.push({
    path: "/walletRecord",
    query: {
    }
  });
}

const onWithdraw = (item) => {
  showBottom.value = true
  walletObj.value = item
}

const onSubmit = async () => {
  try {
    if (!params.value.money) {
      return showToast(t("pleaseWithdrawalAmount"));
    }
    if (Number(params.value.money) > walletObj.value.balance) {
      return showToast(t("insufficientWithdrawalAmount"));
    }
    loading.value = true;
    let res = await Async_withdraw({
      coin: walletObj.value.symbol,
      money: Number(params.value.money),
    })
    if (res) {
      showBottom.value = false
      showToast(t("withdrawSuccessfully"));
    }
    getList();
  } catch (error) {
    console.log(error)
  } finally {
    loading.value = false;
    params.value.money = ""
  }
};


</script>
<style scoped lang="scss">
.tips {
  color: #fff;
  text-align: right;
  margin: 12px;
  font-size: 14px;
  color: #f6d664;
  display: inline-block;
  padding: 0 6px;
  border-radius: 4px;
  font-size: 16px;
  text-align: right;
}

.wallet {
  display: flex;
  justify-content: flex-start;
  padding: 12px;
  border-bottom: 1px solid #1c1c1e;

  .logo {
    width: 45px;
    height: 45px;
    border-radius: 50%;
    margin-right: 12px;
  }

  .content {
    // width: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;

    .top {
      font-size: 16px;
      color: #fff;
      width: 100%;
      display: flex;
      justify-content: space-between;
    }

    .bottom {
      font-size: 12px;
      color: #777779;
      width: 100%;
      display: flex;
      justify-content: space-between;

      .diffFee {
        font-style: normal;

        &.red {
          color: red;
        }

        &.green {
          color: green;
        }
      }
    }
  }

  .withdraw {
    display: flex;
    align-items: center;
    width: 30px;
    margin-left: 12px;
    color: #f6d664;
    font-size: 26px;
    margin-left: 8px;

    &:last-child {
      margin-left: 0;
    }
  }

  .right {
    display: block;
    display: flex;
    justify-content: center;
    align-items: center;
    // margin-left: 8px;
    color: #f6d664;
    font-size: 12px;
  }
}


.van-cell-group {
  margin: 0;
}

.form {
  .submit {
    width: 100%;
    padding: 20px 0;
    display: flex;
    align-items: center;

    .btn {
      width: 100%;
      height: 42px;
      background: linear-gradient(to right, #ffe07b, #e8a22d);
      font-size: 15px;
      font-family: PingFangSC-Semibold, PingFang SC;
      font-weight: 600;
      color: #ffffff;
      border-radius: 20px;
    }
  }
}

p {
  text-align: center;
  font-size: 15px;
  color: #fff;
  margin-bottom: 20px;
}
</style>
